package com.shuoyue.ycgk.ui.questionbank.practice.estamate;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.view.MotionEvent;
import android.view.View;

import com.shuoyue.ycgk.base.BaseMvpFragment;
import com.shuoyue.ycgk.entity.QuestionParent;
import com.shuoyue.ycgk.entity.QuestionSimple;
import com.shuoyue.ycgk.entity.UploadEstimateSucInfo;
import com.shuoyue.ycgk.entity.UserAnswerPager;
import com.shuoyue.ycgk.entity.UserTestPaper;
import com.shuoyue.ycgk.ui.estimate.EsSourtActivity;
import com.shuoyue.ycgk.ui.questionbank.QuestionUtil;
import com.shuoyue.ycgk.ui.questionbank.UploadAnswerContract;
import com.shuoyue.ycgk.ui.questionbank.UserPaperQuestionContract;
import com.shuoyue.ycgk.ui.questionbank.base.BaseQuestionActivity;
import com.shuoyue.ycgk.utils.TimeFomatUtil;
import com.shuoyue.ycgk.utils.XToast;
import com.shuoyue.ycgk.views.dialog.AnswerCardDialog;
import com.shuoyue.ycgk.views.dialog.common.HintConfirmDialog;

import java.util.ArrayList;
import java.util.List;
import java.util.TimerTask;

/**
 * \
 * 考题
 */
public class EstamateQuestionActivity extends BaseQuestionActivity
        implements UserPaperQuestionContract.View, UploadAnswerContract.View {
    UploadAnswerContract.Presenter uploadAnswerPresenter;

    long useTimeMul;//已用时毫秒数
    UserPaperQuestionContract.Presenter spePresenter;
    UserTestPaper userpaper;
    boolean isUploadAnswer = false;
    int evaId;

    @Override
    protected void initView() {
        super.initView();
        card.setVisibility(View.VISIBLE);
        time.setVisibility(View.VISIBLE);
        userpaper = (UserTestPaper) getIntent().getSerializableExtra("pager");
        name.setText(userpaper.getName());
    }

    public static void start(Context source,
                             UserTestPaper pager,
                             long useTimeMul, int evaId) {
        source.startActivity(new Intent(source, EstamateQuestionActivity.class)
                .putExtra("pager", pager)
                .putExtra("useTimeMul", useTimeMul)

                .putExtra("evaId", evaId)

        );
    }

    @Override
    public Class getClassT() {
        return EstamateQuestionMateralFragment.class;
    }

    @Override
    public Class getClassK() {
        return EstamateQuestionSimpleFragment.class;
    }

    @Override
    public void initData() {
        super.initData();
        evaId = getIntent().getIntExtra("evaId", 0);
        useTimeMul = getIntent().getLongExtra("useTimeMul", 0);
        spePresenter = new UserPaperQuestionContract.Presenter();
        spePresenter.attachView(this);
        uploadAnswerPresenter = new UploadAnswerContract.Presenter();
        uploadAnswerPresenter.attachView(this);
        spePresenter.getQuestions(userpaper.getMemberTestPaperId(), 4, userpaper.getType());
    }


    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        if (questionFragmentAdapter != null) {
            BaseMvpFragment fragment = questionFragmentAdapter.getCurrentFragment();
            if (fragment instanceof EstamateQuestionMateralFragment) {
                ((EstamateQuestionMateralFragment) fragment).gesture.onTouchEvent(event);
            }
        }
        return super.dispatchTouchEvent(event);
    }

    @Override
    public void lastOne() {
        showAnswerCard();
    }

    @Override
    public void back() {
        finish();
    }


    final int TIMER_RSPREAD = 500;//计时器间隔
    Handler handler = new Handler();
    TimerTask clock = new TimerTask() {
        @Override
        public void run() {
            handler.postDelayed(clock, TIMER_RSPREAD);
            useTimeMul += TIMER_RSPREAD;
            runOnUiThread(() -> {
                String useTime = TimeFomatUtil.formatmmss(useTimeMul / 1000);
                time.setText(useTime);
                if (answerCardDialog != null) {
                    answerCardDialog.setUseTime(useTime);
                }
            });
        }
    };

    AnswerCardDialog answerCardDialog;

    @Override
    public void showAnswerCard() {
        if (answerCardDialog == null) {
            answerCardDialog = new AnswerCardDialog(mContext, questionParents);
            answerCardDialog.addSelectCall(new AnswerCardDialog.AnswerCardCallBack() {
                @Override
                public void select(QuestionSimple questionSimple) {
                    goDesPosition(questionSimple);
                }

                @Override
                public void submit() {
                    UserAnswerPager userAnswerPager = QuestionUtil.getUserAnswerPager(questionParents,
                            userpaper.getMemberTestPaperId(), userpaper.getType());
                    if (userAnswerPager == null) {
                        XToast.toast(mContext, "没有答案可以提交");
                    } else {
                        userAnswerPager.setTime(useTimeMul / 1000);
                        userAnswerPager.setIsSubmit(1);
                        uploadAnswerPresenter.uploadAnswer(userAnswerPager, 4, false);
                    }

                }
            });
        }
        answerCardDialog.show();
    }


    @Override
    public void onResume() {
        super.onResume();
        handler.postDelayed(clock, TIMER_RSPREAD);
    }

    @Override
    public void onPause() {
        super.onPause();
        handler.removeCallbacks(clock);
    }

    @Override
    public void finish() {
        //TODO 结束前提交信息
        if (isUploadAnswer) {
            EsSourtActivity.start(mContext, evaId, userpaper.getMemberTestPaperId(), userpaper.getName());
            super.finish();
        } else {
            HintConfirmDialog hintConfirmDialog = new HintConfirmDialog(mContext,
                    "提示",
                    "退出自动交卷，是否退出？");
            hintConfirmDialog.setOkListener(v -> {
                UserAnswerPager userAnswerPager = QuestionUtil.getUserAnswerPager(questionParents, userpaper.getMemberTestPaperId(), userpaper.getType());
                if (userAnswerPager == null) {
                    EsSourtActivity.start(mContext, evaId, userpaper.getMemberTestPaperId(), userpaper.getName());
                    super.finish();
                } else {
                    userAnswerPager.setTime(useTimeMul / 1000);
                    userAnswerPager.setIsSubmit(1);
                    uploadAnswerPresenter.uploadAnswerNoView(userAnswerPager, 4, true);

                }
                EsSourtActivity.start(mContext, evaId, userpaper.getMemberTestPaperId(), userpaper.getName());
                super.finish();
            });
            hintConfirmDialog.show();
        }
    }

    @Override
    public void setQuestions(List<QuestionParent> specialPager) {
        setThisActData(specialPager, false);
        QuestionSimple des = getLastDonePos();
        if (des != null) {
            goDesPosition(des);
        }
    }

    @Override
    public void uploadSuc(boolean isNext) {

    }

    @Override
    public void uploadEstimatteSuc(boolean isNext, UploadEstimateSucInfo uploadEstimateSucInfo, int submit) {
        isUploadAnswer = true;
//        XToast.toast(mContext, "答案已提交");
//        if (isNext) {
//            EsSourtActivity.start(mContext,evaId,userpaper.getMemberTestPaperId());
//
//            super.finish();
//        } else {
//            if (uploadEstimateSucInfo != null) {
//                EsSourtActivity.start(mContext, uploadEstimateSucInfo.getId(), uploadEstimateSucInfo.getTestPaperId());
////            EstamateCountActivity.start(mContext, userpaper.getName(), (ArrayList<QuestionParent>) questionParents);
//            }
//            EsSourtActivity.start(mContext,evaId,userpaper.getMemberTestPaperId());
//            super.finish();
//        }
        EsSourtActivity.start(mContext, evaId, userpaper.getMemberTestPaperId(), userpaper.getName());
        super.finish();
    }

    @Override
    public void uploadFail(boolean isNext, int submit) {
        XToast.toast(mContext, "答案提交失败");

    }
}
